package br.com.upper.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import br.com.upper.business.Facade;
import br.com.upper.entity.InstanceType;
import br.com.upper.entity.TestConfiguration;
import br.com.upper.entity.TestResult;
import br.com.upper.entity.Transaction;
import br.com.upper.entity.VolumeType;

public class Config extends UpperSupport {

	private TestConfiguration configuration;
	private Integer volumeTypeValue;
	private List<InstanceType> instanceTypes;
	private List<VolumeType> volumeTypes;
	private String[] transacoesNome;
	private String[] transacoesMaxTime;
	private Integer instanceTypeIndex;		

	@Override
	public void prepare() throws Exception {
		
		this.configuration = Facade.getInstance().getTestConfiguration();
		
		if(configuration == null) {
			this.configuration = new TestConfiguration();			
		}
		
		this.instanceTypes = Facade.getInstance().findAllInstanceType();
		this.volumeTypes = VolumeType.getVolumeTypes();
	}
	
	@Override
	public String execute() throws Exception {
				
		if(isPostMethod()) {
			
			VolumeType volumeType = VolumeType.getVolumeType(volumeTypeValue);
			this.configuration.setVolumeType(volumeType);
			
			this.configuration.setTransactions(new ArrayList<Transaction>());
			
			Facade.getInstance().deleteAllTransactions();		
			
			if(this.transacoesNome != null && this.transacoesNome.length > 0) {				
				for(int i = 0; i < this.transacoesNome.length; i++) {					
					Transaction t = new Transaction();
					t.setNome(this.transacoesNome[i]);
					t.setTimeExecution(Long.valueOf(this.transacoesMaxTime[i]));
					t.setConfiguration(this.configuration);
					this.configuration.getTransactions().add(t);
					Facade.getInstance().saveTransaction(t);
				}				
			}
			
			Facade.getInstance().saveTestConfiguration(this.configuration);
			
			atualizarPropertie();

			return SUCCESS;
			
		} else {
			
			long currentTime = System.currentTimeMillis();
			getSessionMap().put("initTotalTime", currentTime);
			getSessionMap().put("initConfigTime", currentTime);
			
			TestResult result = (TestResult)getSessionMap().get("testResult");
			result.setInitTimestamp(new Date());			
			Facade.getInstance().saveTestResult(result);
			
			return SUCCESS;	
		}

	}
	
	private void atualizarPropertie() throws IOException {
		
		String propertiePath = this.configuration.getJMeterDirectory() + File.separator + "bin" + File.separator + "user.properties";
		
        FileInputStream in = new FileInputStream(propertiePath);
        Properties props = new Properties();
        props.load(in);
        in.close();

        FileOutputStream out = new FileOutputStream(propertiePath);
        
        props.setProperty(this.configuration.getLogTransactionsPathKey(),this.configuration.getLogTransactionsPath().toString());
        props.setProperty(this.configuration.getLogResourcesPathKey(),this.configuration.getLogResourcesPath().toString());        
        props.setProperty(this.configuration.getDbURLJMeterKey(),this.configuration.getDbUrl());
        props.setProperty(this.configuration.getDbDriverJMeterKey(),this.configuration.getDbDriver());
        props.setProperty(this.configuration.getDbUserJMeterKey(),this.configuration.getDbUser());
        props.setProperty(this.configuration.getDbPasswordJMeterKey(),this.configuration.getDbPassword());
        props.setProperty(this.configuration.getDbMaxConnJMeterKey(),this.configuration.getDbMaxConn());
        props.setProperty(this.configuration.getDbTimeOutGroupJMeterKey(),this.configuration.getDbTimeOutGroup());
        props.setProperty(this.configuration.getDbCleanIdleIntervalJMeterKey(),this.configuration.getDbCleanIdleInterval());
        props.setProperty(this.configuration.getDbConnTimeoutJMeterKey(),this.configuration.getDbConnTimeout());
        props.setProperty(this.configuration.getDbTestQueryJMeterKey(),this.configuration.getDbTestQuery());
        props.setProperty(this.configuration.getNumThreadsJMeterKey(),this.configuration.getNumThreads().toString());
        props.setProperty(this.configuration.getInitTimeJMeterKey(),this.configuration.getInitTime().toString());
        props.setProperty(this.configuration.getIterationsJMeterKey(),this.configuration.getIterations().toString());

        props.store(out, null);
        out.close();
	}

	public TestConfiguration getConfiguration() {
		return configuration;
	}

	public List<InstanceType> getInstanceTypes() {
		return instanceTypes;
	}

	public void setInstanceTypes(List<InstanceType> instanceTypes) {
		this.instanceTypes = instanceTypes;
	}

	public Integer getInstanceTypeIndex() {
		return instanceTypeIndex;
	}

	public void setInstanceTypeIndex(Integer instanceTypeIndex) {
		this.instanceTypeIndex = instanceTypeIndex;
	}

	public Integer getVolumeTypeValue() {
		return volumeTypeValue;
	}

	public void setVolumeTypeValue(Integer volumeTypeValue) {
		this.volumeTypeValue = volumeTypeValue;
	}

	public List<VolumeType> getVolumeTypes() {
		return volumeTypes;
	}

	public void setVolumeTypes(List<VolumeType> volumeTypes) {
		this.volumeTypes = volumeTypes;
	}

	public String[] getTransacoesNome() {
		return transacoesNome;
	}

	public void setTransacoesNome(String[] transacoesNome) {
		this.transacoesNome = transacoesNome;
	}

	public String[] getTransacoesMaxTime() {
		return transacoesMaxTime;
	}

	public void setTransacoesMaxTime(String[] transacoesMaxTime) {
		this.transacoesMaxTime = transacoesMaxTime;
	}
	
	
	
	
	
}
